/**
 * Depends:
 * utility.json.loader.js
 * utility.json.render.js
 * utility.json.loader.queue.js
 */
$.fn.autoAddWidget = function(options){
	var $this = this;
	options = options || {};
	
	var categorySelector 	= options.categorySelector 		|| '#category';
	var subcategorySelector = options.subcategorySelector 	|| '#subcategory';
	var markSelector 		= options.markSelector 			|| '#mark';
	var modelSelector 		= options.modelSelector 		|| '#model';
	var citySelector 		= options.citySelector 			|| '#city';
	var stateSelector 		= options.stateSelector 		|| '#state';
	var paramsSelector 		= options.paramsSelector 		|| 'p.multioptions';
    
    var switcherSelector    = options.switcherSelector      || 'ul.catalog-category';
	
	/**
	 * 
	 */
	$this._init = function(){
		$this.category		= $(this).find(categorySelector);
		$this.subcategory 	= $(this).find(subcategorySelector);
		$this.mark 			= $(this).find(markSelector);
		$this.model 		= $(this).find(modelSelector);
		$this.city  		= $(this).find(citySelector);
		$this.state 		= $(this).find(stateSelector);
		$this.params		= $(this).find(paramsSelector);
        $this.switcher      = $(switcherSelector);
	};
	
	/**
	 * 
	 */
	$this._bindEvents = function(){
		$this.category.change(function(){
			$this.loadSubcategories();
			$this.loadMarks();
			$this.mark.change();
			$this.loadParams();
		});
		
		$this.state.change(function(){
			$this.loadCities();
			$this.city.change();
		});
		
		$this.mark.change(function(){
			$this.loadModels();
			$this.model.change();
		});

        $this.switcher.ready(function(){
            var cat = window.location.hash.replace(/\D*/ , "");
            $this.runSwitcher(cat);
        });

        $this.switcher.find('a').click(function(){
            if($(this).hasClass('active')) return;
            $this.runSwitcher($(this).attr('id').replace(/\D*/, ""));
        });
	};

    $this.runSwitcher = function(cat){
        if(cat>0){
                $this.category.val(cat).change();
                $this.switcher.find('a').removeClass('active');
                window.location.hash = 'category/'+cat;
                $('#cat_'+cat).addClass('active');
        }
    };

	/**
	 * 
	 */
	$this.loadMarks = function(){
		var items = utility.json.render({
			data : utility.json.loader({
				url : 'ajax/get-marks',
				data : {category : $this.category.val()}
			})
		});
		$this.mark.empty().append($(items));
	};
	
	/**
	 * 
	 */
	$this.loadCities = function(){
		var items = utility.json.render({
			data : utility.json.loader({
				url : 'ajax/get-cities',
				data : {identifier : $this.state.val()}
			})
		});
		
		$this.city.empty().append($(items));
	};
	
	/**
	 * 
	 */
	$this.loadModels = function(){
		var items = utility.json.render({
			data : utility.json.loader({
				url : 'ajax/get-models',
				data : {identifier : $this.mark.val(), subcategory : $this.subcategory.val()}
			})
		});
		$this.model.empty().append($(items));
	};
	
	/**
	 * 
	 */
	$this.loadParams = function(){
		var items = utility.json.render({
			data : utility.json.loader({
				url : 'ajax/get-params',
				data : {identifier : $this.category.val()}
			}),
			snippet : '<label for="params-$value$"><input type="checkbox" value="$value$" id="params-$value$" name="params[]">$name$</label>'
		});
		$this.params.empty().append($(items));
	};
	
	/**
	 * 
	 */
	$this.loadSubcategories = function(){
		var items = utility.json.render({  
			data : utility.json.loader({
				url : 'ajax/get-subcategories',
				data : {identifier : $this.category.val()}
			})
		});
		$this.subcategory.empty().append($(items));
        $this.subcategory.change();
	};
	
	/**
	 * Implementation
	 */
	$this._init();
	$this._bindEvents();
	return $(this);
};